import {
  // 美股期权成交
  Upload,
  GetByDate,
  TurnOverMonitorList,
  GetDateList,
  tradingUpdateOption,
  AddNewOption,
  clear,
  Delete,

  // 美股个股
  aUploadcode,
  getcodelist,
  clearcode,
  codeDateList,

  // 盘中期权接口
  // TradingUploadOrder,
  // TradingGetList,
  // TradingGetByDate,
  // TradingGetDateList
} from '../../../services/monitor_options_service';

export default {
  namespace: 'monitor',
  state: {
    dataSearchList: [],
    data: {
      list: [],
      pagination: {},
    },
    data2: {
      list: {},
      pagination: {},
    },
    filterData: {
      list: [],
    },

    codeDateData: {
      code: 1,
      message: '',
      data: [],
    },

    dateListData: {
      code: '',
      message: '',
      data: [],
    },
  },

  effects: {
    // 盘中期权
    // *TradingGetList({ payload, callback }, { call, put }) {
    //   let response = yield call(TradingGetList, payload); if (typeof (response) === 'undefined') { return; }
    //   for (let i = 0; i < response.data.length; i++) {
    //     if (response.data[i].code === '成交合计') {
    //       response.data[i].id = 9998;
    //     } if (response.data[i].code === '未成交合计') {
    //       response.data[i].id = 9999;
    //     }
    //   }
    //   yield put({
    //     type: 'saveInOptions',
    //     payload: response,
    //   });
    //   if (callback) {
    //     callback(response);
    //   }
    // },
    // *TradingGetByDate({ payload, callback }, { call, put }) {
    //   let response = yield call(TradingGetByDate, payload); if (typeof (response) === 'undefined') { return; }
    //   for (let i = 0; i < response.data.length; i++) {
    //     if (response.data[i].code === '成交合计') {
    //       response.data[i].id = 9998;
    //     } if (response.data[i].code === '未成交合计') {
    //       response.data[i].id = 9999;
    //     }
    //   }
    //   yield put({
    //     type: 'saveInOptions',
    //     payload: response,
    //   });
    //   if (callback) {
    //     callback(response);
    //   }
    // },
    // *TradingUploadOrder({ payload, callback }, { call, put }) {
    //   const response = yield call(TradingUploadOrder, payload); if (typeof (response) === 'undefined') { return; }
    //   yield put({
    //     type: 'saveInOptions',
    //     payload: response,
    //   });
    //   if (callback) {
    //     callback(response);
    //   }
    // },
    // *TradingGetDateList({ payload, callback }, { call, put }) {
    //   const response = yield call(TradingGetDateList, payload); if (typeof (response) === 'undefined') { return; }
    //   yield put({
    //     type: 'saveInOptions',
    //     payload: response,
    //   });
    //   if (callback) {
    //     callback(response);
    //   }
    // },

    // 美股期权成交
    *Upload({ payload, callback }, { call, put }) {
      const response = yield call(Upload, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveTrading',
        payload: response,
      });
      if (callback) {
        callback(response);
      }
    },
    *TurnOverMonitorList({ payload, callback }, { call, put }) {
      const response = yield call(TurnOverMonitorList, payload);
      if (typeof response === 'undefined') {
        return;
      }

      let openSumYes = 0;
      let openSumNo = 0;
      for (let i = 0; i < response.data.length; i++) {
        if (response.data[i].turnflag) {
          openSumYes += response.data[i].openpotential ? response.data[i].openpotential * 1 : 0;
        } else {
          openSumNo += response.data[i].openpotential ? response.data[i].openpotential * 1 : 0;
        }
      }
      response.openSumYes = openSumYes;
      response.openSumNo = openSumNo;
      yield put({
        type: 'saveTrading',
        payload: response,
      });
      if (callback) {
        callback(response);
      }
    },
    *GetByDate({ payload, callback }, { call, put }) {
      const response = yield call(GetByDate, payload);
      if (typeof response === 'undefined') {
        return;
      }
      let openSumYes = 0;
      let openSumNo = 0;
      for (let i = 0; i < response.data.length; i++) {
        if (response.data[i].turnflag) {
          openSumYes += response.data[i].openpotential ? response.data[i].openpotential * 1 : 0;
        } else {
          openSumNo += response.data[i].openpotential ? response.data[i].openpotential * 1 : 0;
        }
      }
      response.openSumYes = openSumYes;
      response.openSumNo = openSumNo;
      yield put({
        type: 'saveTrading',
        payload: response,
      });
      if (callback) {
        callback(response);
      }
    },
    *AddNewOption({ payload, callback }, { call, put }) {
      const response = yield call(AddNewOption, payload);
      if (typeof response === 'undefined') {
        return;
      }

      yield put({
        type: 'saveTrading',
        payload: response,
      });
      if (callback) {
        callback(response);
      }
    },
    *tradingUpdateOption({ payload, callback }, { call, put }) {
      const response = yield call(tradingUpdateOption, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveTrading',
        payload: response,
      });
      if (callback) {
        callback(response);
      }
    },
    *GetDateList({ payload, callback }, { call, put }) {
      const response = yield call(GetDateList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      // yield put({
      //   type: 'saveTrading',
      //   payload: response,
      // });
      if (callback) {
        callback(response);
      }
    },
    *clear({ payload }, { call, put }) {
      const response = yield call(clear, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveTrading',
        payload: response,
      });
    },

    *Delete({ payload, callback }, { call, put }) {
      const response = yield call(Delete, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveTrading',
        payload: response,
      });
      if (callback) {
        callback(response);
      }
    },
    // 美股个股
    *aUploadcode({ payload, callback }, { call, put }) {
      const response = yield call(aUploadcode, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveA',
        payload: response,
      });
      if (callback) {
        callback(response);
      }
    },

    *getcodelist({ payload }, { call, put }) {
      const response = yield call(getcodelist, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveA',
        payload: response,
      });
    },
    *clearcode({ payload }, { call, put }) {
      const response = yield call(clearcode, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveA',
        payload: response,
      });
    },

    *CodeDateList({ payload }, { call, put }) {
      const response = yield call(codeDateList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveCodeDateData',
        payload: response,
      });
      if (callback) {
        callback(response);
      }
    },
  },

  reducers: {
    // saveInOptions(state, action) {
    //   return {
    //     ...state,
    //     data: action.payload,
    //   };
    // },
    saveTrading(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    saveO(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    saveco(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    saveA(state, action) {
      return {
        ...state,
        data2: action.payload,
      };
    },
    saveFilter(state, action) {
      return {
        ...state,
        filterData: action.payload,
      };
    },

    saveCodeDateData(state, action) {
      return {
        ...state,
        codeDateData: action.payload,
      };
    },

    GetDateList(state, action) {
      return {
        ...state,
        dateListData: action.payload,
      };
    },
  },
};
